<?php
namespace Home\Controller;

class OrderController extends CommonController{

	public function index(){
		
		$this->display();
	}
	//编辑订单
	public function edit_order(){
		if(IS_POST){
			$info = I('post.');
			//验证商品真实性
			$goods_info = D('Goods')->where(array('id'=>$info['goods_id'],'status'=>'1','delete_status'=>'1'))->find();
			$goods_info || $this->error('抱歉，该商品不存在或已下架',U('Goods/index'));
			if($goods_info['type'] == '1'){
				//验证规格
				$info['spec_id'] || $this->error('请选择商品规格');
				//规格信息
				$spec_info = D('GoodsSpec')->where(array('id'=>$info['spec_id']))->find();
				$info['order_num'] > $spec_info['store'] && $this->error('抱歉，该商品库存不足');
			}else{
				$info['order_num'] > $goods_info['store'] && $this->error('抱歉，该商品库存不足');
			}
			//记录到session
			session('basic_order',$info);
			//查看是否实名认证
			$this->user['is_validate'] != '1' && $this->error('您尚未实名认证，请先填写认证信息',U('User/auth_base_info'));
			
			$this->success('提交成功!',U('Order/edit_order'));
		}else{
			$basic_order = session('basic_order');
			$basic_order || $this->error_msg('订单信息已失效，请重新下单',U('Index/index'));
			//地址信息
			$address_info = D('Address')->get_address($this->user['id'],I('address_id'));
			//商品信息
			$goods_info = D('Goods')->where(array('id'=>$basic_order['goods_id']))->find();
			//规格信息
			$spec_info = D('GoodsSpec')->where(array('id'=>$basic_order['spec_id']))->find();
			//系统配置
			$global_info = D('Other')->find();
			//order价格
			($basic_order['service'] == '1') && $basic_order['order_price'] = $goods_info['price'] * $basic_order['order_num'];
			($basic_order['service'] == '2') && $basic_order['order_price'] = $goods_info['vip_price'];
			($basic_order['service'] == '3') && $basic_order['order_price'] = $goods_info['per_first_price'] + $global_info['deposit'];

			$this->assign('address_info',$address_info);
			$this->assign('goods_info',$goods_info);
			$this->assign('spec_info',$spec_info);
			$this->assign('global_info',$global_info);
			$this->assign('basic_order',$basic_order);
			$this->display();
		}
	}
	//提交订单
	public function sub_order(){
		if (IS_POST) {
			if(!$order_info = D('Order')->create_order(I('post.')))$this->error(D('Order')->getError());
			session('basic_order',null);
			session('order_info',$order_info);
			$this->success('下单成功！',U('Order/sub_order'));
		}else {
			I('get.order_id') && session('order_info',D('Order')->where(array('id'=>(int)I('get.order_id')))->find());
			session('order_info') && $order_info = session('order_info');
			$order_info || $this->error_msg('没有对应的订单信息',U('Index/index'));
			//验证该订单是否已经被支付
			$this->check_pay($order_info['order_sn']) && $this->redirect('Order/order_send');
			
			$this->assign('order_info',$order_info);
			$this->assign('goods_info',D('Goods')->where(array('id'=>$order_info['goods_id']))->find());
			$this->display();
		}
	}
	//待支付订单
	public function order_pay(){
        $list = D('Order')->where(array('user_id'=>$this->user['id'],'refund_status'=>'1','status'=>'1','order_type'=>array('in','1,2')))->order('create_time desc')->select();
		$this->assign('list',$list);
		$this->display();
	}
	//已收货/已完成订单
	public function order_complete(){
        $list = D('Order')->where(array('user_id'=>$this->user['id'],'refund_status'=>'1','status'=>'4','order_type'=>array('in','1,2')))->order('create_time desc')->select();
        $this->assign('list',$list);
		
		$this->display();
	}
	//待收货订单
	public function order_confirm(){
         $list = D('Order')->where(array('user_id'=>$this->user['id'],'refund_status'=>'1','status'=>'3','order_type'=>array('in','1,2')))->order('create_time desc')->select();
        $this->assign('list',$list);
		$this->display();
	}

	//物流接口
	public function logistics_details(){
		$info = D('Order')->where(array('id'=>I('order_id')))->find();
		$wl = pinyin1($info['logistics_company']);   //取快递公司的首字母
		$key = '1c8e4867536c5d222dbb7331434195fd';
		$type = 'json';
		$res = curl_request('http://v.juhe.cn/exp/index?com='.$wl.'&no='.$info['logistics_number'].'&type='.$type.'&key='.$key);
		$arr = json_decode($res,true);
		$this->assign('info',$arr);
		$this->assign('pic',$info['goods_pic']);
		$this->display();

	}
	//退货订单列表
	public function order_refund(){
		 $list = D('Order')->where(array('user_id'=>$this->user['id'],'refund_status'=>'1','status'=>array('GT',4),'order_type'=>array('in','1,2')))->order('create_time desc')->select();
        $this->assign('list',$list);
		
		$this->display();
	}
	//待发货订单列表
	public function order_send(){
         $list = D('Order')->where(array('user_id'=>$this->user['id'],'status'=>'2','order_type'=>array('in','1,2')))->order('create_time desc')->select();
        $this->assign('list',$list);
		$this->display();
	}
	//订单详情
	public function order_detail(){
		$id = (int)I('get.id');
		$order_info = D('Order')->relation(true)->where(array('id'=>$id))->find();
		$order_info || $this->error_msg('非法的参数',U('User/index'));
		$this->assign('order_info',$order_info);
		$this->display();
	}
	//取消订单
	public function cancel_order(){
		$order_id = (int)I('post.order_id');
		$order_id || $this->error('参数有误');
		$result = D('Order')->delete($order_id);
		$result ? $this->success('订单已取消',U('Order/order_pay')) : $this->error('系统繁忙，操作失败！');
	}
	//申请退款
	public function reply_refundment(){
		$order_id = (int)I('post.order_id');
		$order_id || $this->error('参数有误');
		$result = D('Order')->where(array('id'=>$order_id))->save(array('refund_status'=>'5'));
		$result ? $this->success('已提交退款申请',U('Order/order_send')) : $this->error('系统繁忙，操作失败！');
	}
	//确认收货
	public function receive_order(){
		$id = (int)I('get.id');
		$order_id = (int)I('get.order_id');
		$bike_info = D('Bike')->find($id);
		$order_info = D('Order')->find($order_id);
		$bike_info || $this->error_msg('车辆信息丢失，请重新扫码');
		$order_info || $this->error_msg('订单参数有误');
		($bike_info['status'] == '2') && $this->error_msg('抱歉，该车已经有主人了');
		($order_info['status'] != '3') && $this->error_msg('抱歉，此订单暂不可确认收货');
		//更新车辆信息
		$update_data = array();
		$update_data['order_id'] = $order_id;
		$update_data['user_id'] = $order_info['user_id'];
		$update_data['user_name'] = $order_info['contact_user'];
		$update_data['user_phone'] = $order_info['contact_phone'];
		$update_data['status'] = '2';
		$update_data['bund_time'] = time();
		D('Bike')->where(array('id'=>$bike_info['id']))->save($update_data);
		//更新订单信息
		D('Order')->where(array('id'=>$order_info['id']))->save(array('status'=>'4'));
		
		$this->success_msg('恭喜您！确认收货成功，<br/>订单已完成',U('Order/order_complete'));
	}
	//检验是否支付成功
	private function check_pay($order_sn){
		vendor("Wxpay.WxPayApi");
		$payObj = new \WxPayOrderQuery();
		$payObj->SetOut_trade_no($order_sn);
		$result = \WxPayApi::orderQuery($payObj);
		if(array_key_exists("trade_state", $result) && $result["trade_state"] == "SUCCESS") return true;
		return false;
	}

	//订单支付回调
	public function pay_back(){
		$order_info = session('order_info');
		if($this->check_pay($order_info['order_sn'])){
			$order_info = D('Order')->where(array('id'=>$order_info['id']))->find();
			if($order_info['pay_status'] == '1'){
				$save_data = array();
				$save_data['pay_status'] = $order_info['service'] == '3'?'2':'3';//1.未支付 2.支付首款/押金 3.已支付
				$save_data['pay_cash'] = $order_info['order_price'];
				$save_data['pay_time'] = time();
				$save_data['status'] = 2;
				D('Order')->where(array('id'=>$order_info['id']))->save($save_data);
				//支付日志
				$log_data = array();
				$log_data['user_id'] = $order_info['user_id'];
				$log_data['order_id'] = $order_info['id'];
				$log_data['pay_type'] = '1';
				$log_data['pay_value'] = $order_info['order_price'];
				D('PayLog')->add($log_data);
				//分期付款记录
				if($order_info['service'] == '3'){
					$goods_info = D('Goods')->where(array('id'=>$order_info['goods_id']))->find();
					$repayment = array();
					$repayment['user_id'] = $order_info['user_id'];
					$repayment['order_id'] = $order_info['id'];
					$repayment['remain_cash'] = $order_info['goods_price']-$order_info['order_price'];
					$repayment['remain_days'] = $goods_info['per_days'];
					$repayment['per_days'] = $goods_info['per_days'];
					$repayment['per_cash'] = $goods_info['per_price'];
					D('Repayment')->add($repayment);
				}
				//销量增加
				D('Goods')->where(array('id'=>$order_info['goods_id']))->setInc('sale_count',$order_info['order_num']);
				
				session('order_info',null);
			}
		}
		$this->redirect('Order/order_send');
	}
}